package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.activity.client.ActivityFeignClient;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.activity.SeckillGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
public class SeckillController {

    //  获取远程调用
    @Autowired
    private ActivityFeignClient activityFeignClient;

    /**
     * 秒杀列表
     * @param model
     * @return
     */
    @GetMapping("seckill.html")
    public String seckillIndex(Model model){
        Result<List<SeckillGoods>> result = activityFeignClient.findAll();
        //  all 存储集合，页面需要的key 都在实现类已经封装好！
        //  model.addAllAttributes()
        model.addAttribute("list",result.getData());
        //  秒杀列表页面
        return "seckill/index";
    }

    /**
     * 秒杀详情
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("seckill/{skuId}.html")
    public String seckillItem(@PathVariable Long skuId,Model model){
        //  存储item
        Result<SeckillGoods> result = activityFeignClient.getSeckillGoods(skuId);
        model.addAttribute("item",result.getData());
        //  秒杀详情页面
        return "seckill/item";
    }

    /**
     * 来到排队页面
     * http://activity.gmall.com/seckill/queue.html?skuId=24&skuIdStr=c81e728d9d4c2f636f067f89cc14862c
     * @return
     */
    @GetMapping("seckill/queue.html")
    public String queue(HttpServletRequest request){
        //  需要存储skuId , skuIdStr 抢购码
        request.setAttribute("skuId",request.getParameter("skuId"));
        request.setAttribute("skuIdStr",request.getParameter("skuIdStr"));
        //  返回排队页面
        return "seckill/queue";
    }

    /**
     * 秒杀订单结算页
     * @param model
     * @return
     */
    @GetMapping("seckill/trade.html")
    public String trade(Model model){
        //  userAddressList detailArrayList totalAmount totalNum
        Result<Map> result = activityFeignClient.seckillTradeData();
        //  判断
        if (result.isOk()){
            //  保存数据
            model.addAllAttributes(result.getData());
            //  返回订单结算页
            return "seckill/trade";
        } else {
            //  返回失败页面
            model.addAttribute("message","展示订单结算页面失败");
            return "seckill/fail";
        }

    }
}
